package com.njtoyo.taxi.admin.cmb;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njtoyo.taxi.admin.mapper.master.entity.BankOutlet;
import com.njtoyo.taxi.admin.mapper.master.entity.SysRegion;
import com.njtoyo.taxi.admin.mapper.master.mapper.SysRegionMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.data.BankOutletMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@SpringBootTest
@ActiveProfiles(value = "test")
public class 导入联行号 {

    @Autowired
    private SysRegionMapper regionMapper;
    @Autowired
    private BankOutletMapper bankOutletMapper;

    @Test
    public void test() {
        ExcelReader reader = ExcelUtil.getReader("/Users/saber/Desktop/有滴资料/招商银行/联行号new_20221031083154.xlsx");
        List<Map<String, Object>> readAll = reader.readAll();
        List<SysRegion> regions = regionMapper.selectList(Wrappers.<SysRegion>lambdaQuery().eq(SysRegion::getLayerLevel, 2));
        Map<String, SysRegion> regionMap = regions.stream().collect(Collectors.toMap(region -> region.getName(), Function.identity(), (k1, k2) -> k1));
        CollUtil.forEach(readAll, ((map, index) -> {
            String city = (String) map.get("城市名称");
            SysRegion region = regionMap.get(city);
            if (ObjectUtil.isNotNull(region)) {
                BankOutlet bankOutlet = new BankOutlet();
                bankOutlet.setBankName(MapUtil.getStr(map, "银行类型"));
                bankOutlet.setBankNumber(MapUtil.getStr(map, "银行联行号"));
                bankOutlet.setOutletName(MapUtil.getStr(map, "网点全称"));
                bankOutlet.setRegion_id(region.getRegionId());
                bankOutletMapper.insert(bankOutlet);
            }
            System.out.println("还剩" + (readAll.size() - index) + "条待处理");
        }));
    }
}
